<?
global $tpl;
include("./class/resize_image.php");
switch($act){
	case "detail":
		Detail();
		$tpl="detail";
		break;
	
	case "special":
		Special();
		$tpl="special";
		break;	

	case 'search':
		Search();
		$title_page = RESULT_SEARCH;
		$tpl="list";
		break;
	
	case 'favourite':
		Favourite();
		$title_page = FAVOURITE_LIST;
		$tpl = 'list';
		break;
	
	default:
		ShowList();
		$tpl="list";
}

function Favourite()
{
	if(isset($_SESSION['member_id']))
	{
		global $db, $page, $plpage, $set_per_page, $c, $title_bar, $articles;
		global $lg, $title_page, $FullUrl;
		
		$title_bar = FAVOURITE_LIST;
		
		$set_per_page = CST_ARTICLE_PAGING_ALL;
		
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from favourite f, articles p where f.fav_article_id = p.id and fav_mem_id = ".$_SESSION['member_id']." order by p.id desc ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage_seo($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);	
		$articles = $db->getAll($sqlstmt);
	}
}

function ShowAll()
{
	global $db,$articles,$page,$plpage,$set_per_page,$c, $cat,$title_bar, $idMenuMain, $newer, $older, $product;
	$id=CleanSQLInjection(trim(isset($_GET['cid'])?$_GET['cid']:''));
	
	$sql = "select * from categories where id=$id";
	$cat = $db->getRow($sql);
	
	$title_bar =  getTitleBar($id, 1, '');
	
	$cmp = '';
	if(isset($_GET['dateCreated'])){
		$dateCreated = CleanSQLInjection(trim($_GET['dateCreated']));
		$cmp = " AND dated = '" . $dateCreated . "' ";
	}
	
	$set_per_page=CST_ARTICLE_PAGING_ALL;
	$cid = CleanSQLInjection(trim(isset($_GET['cid'])?$_GET['cid']:''));
	
	$sql= "select * from articles where active=1 $cmp and name_" . $_SESSION['lg'] . "<>'' and cid in (select id from categories where pid=".$cid.") order by num asc, id desc "; 
	
	$c = $db->numRows($db->query($sql));
	$plpage = plpage($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$articles = $db->getAll($sqlstmt);
}

function ShowList()
{
	global $db,$articles,$page,$plpage,$set_per_page,$c, $cat,$title_bar, $idMenuMain, $newer, $older, $product, $FullUrl, $big_articles;	
	global $cat, $cat1, $cat2, $lg, $prefix_url, $title_page;
	
	if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
	}else{
		$cat = $cat1;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
	}
	
	$title_page = $cat["title_$lg"]; 
	
	$set_per_page = CST_ARTICLE_PAGING_LIST;
	
	if($cat['id'] != '')
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where name_$lg<>'' and  active=1 and cid=".$cat['id']." order by num asc, id desc";
	else
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where name_$lg<>'' and  active=1 order by num asc, id desc";
	//echo $sql;
	$c = $db->numRows($db->query($sql));
	$plpage = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$articles = $db->getAll($sqlstmt);	 //echo $sqlstmt;
}
function Search()
{
	global $db,$articles,$page,$plpage,$set_per_page,$c, $cat,$title_bar, $isSearch, $lg;
	$key = CleanSQLInjection(trim(isset($_GET['key'])?$_GET['key']:''));
	$lg = CleanSQLInjection(trim(isset($_GET['lg'])?$_GET['lg']:'vn')); 
	$isSearch = true;
	
	if(!empty($key))
	{
		$title_bar =  RESULT_SEARCH;
		$set_per_page = CST_ARTICLE_PAGING_SEARCH;
		
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where name_".$lg." like '%".$key."%' or short_".$lg." like '%".$key."%' or content_".$lg." like '%".$key."%' ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);
		$articles = $db->getAll($sqlstmt);
	}
}
function Detail()
{
	global $db, $article, $id_article, $cat, $another_news, $title_bar, $idMenuMain, $page, $plpagecomment, $set_per_page;
	global $cat1, $cat2, $lg, $prefix_url, $title_page, $keywords, $descriptions, $FullUrl, $news_comments, $tpl;
	
	if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
	}else{
		$cat = $cat1;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
	}
		
	$unique_key = CleanSQLInjection(trim(isset($_GET['unique_key'])?$_GET['unique_key']:''));
	$article_key = $unique_key;
	$sql = "select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where unique_key_$lg='$article_key' and cid=" . $cat['id'];
	$article = $db->getRow($sql);
	CountArticleView($article['id'], $db);
	//seo
	$title_page = $article["title_$lg"];
	$keywords = $article["keyword_$lg"];
	$descriptions = $article["des_$lg"];
	
	$sql = "select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where id<" . $article["id"] . " and name_$lg<>'' and cid=".$article['cid']." order by num asc, id desc limit 0,4";
	$another_news = $db->getAll($sql);
	
	$set_per_page = CST_ARTICLE_PAGING_COMMENT;
	$sql = "select *, (select name from member where id = cmt_mem_id) as mem_name from comments where cmt_article_id = ".$article['id']." and cmt_active = 1 order by cmt_id desc";
	
	$plpagecomment = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$news_comments = $db->getAll($sqlstmt);
	//echo $tpl.'3';
}
function Special()
{
	global $db, $article, $id_article, $cat, $another_news, $title_bar, $idMenuMain, $page, $plpagecomment, $set_per_page;
	global $cat1, $cat2, $lg, $prefix_url, $title_page, $keywords, $descriptions, $FullUrl, $news_comments;
		
	$list = $_GET['list'];	
	$sql = "select * from articles where alias_name = '".$list."'";
	$article = $db->getRow($sql);
	
}
function CountArticleView($ArticleId, $db){
	global $UniqueSession;
	$UniqueSession = "ArticlesAreViewed";
	$ArticleIdString = "[" . $ArticleId . "]";
	
	if(!isset($_SESSION[$UniqueSession]))
	{
		$_SESSION[$UniqueSession] = $ArticleIdString;
		
		$sql = "update articles set view = view + 1 where id = $ArticleId";	
		$db->query($sql);
	}
	else
	{
		$mystring = $_SESSION[$UniqueSession];
		$findme = $ArticleIdString;
		$pos = strpos($mystring, $findme);
		
		if($pos === false)
		{
			$_SESSION[$UniqueSession] .= $ArticleIdString;
			
			$sql = "update articles set view = view + 1 where id = $ArticleId";	
			$db->query($sql);
		}
	}
}
?>